package uplb.ics.cs190.e2d.core;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import uplb.ics.cs190.e2d.display.StandardMobileMode;
import uplb.ics.cs190.e2d.io.MobileIO;
import uplb.ics.cs190.e2d.time.StandardTimer;

public abstract class MobileLoader extends MIDlet implements Console {

	protected Game game;
	protected Engine engine;
	
	public MobileLoader() {
		this.engine = Engine.getInstance();
	}
	
	public void setup(Game game) {
		try {
			engine.mountGame(game);
			/* Initialize File IO */
			engine.fileIO = new MobileIO();
			/* Initialize Display */
			javax.microedition.lcdui.Display display = javax.microedition.lcdui.Display.getDisplay(this);
			engine.display = new StandardMobileMode(false, display);
			/* Initialize System Time */
			engine.timer = new StandardTimer();
		} catch(Exception e) {
			System.exit(-1);
		}
	}
	
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		this.stop();
	}

	protected void pauseApp() {
		if(this.engine != null) {
			this.engine.pause();
		}
	}

	protected void startApp() throws MIDletStateChangeException {
		this.start();
	}

	public Game getGame() {
		return this.game;
	}
}